<?php
/**
 * 找好物-好物合集
 */

use Biz\model\ShopCategoryClassify;
use Biz\service\wiki\Wiki;
use Doraemon\service\statSign\Basic as basicStatSign;

class ClassifyWikiCollectionAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_NOTHING;
    const IS_LOG_EXPOSE = 1;

    /**
     * 找好物页列表
     * @return void
     * @throws Exception
     */
    public function main()
    {
        $res = [];

        try {

            $wiki_id = $this->getRequest()->getQuery('wiki_id', 0);
            $first_classify_id = $this->getRequest()->getQuery('classify_id', 0); //以及分类
            $act_from = $this->getRequest()->getQuery('act_from', 'find_wiki'); //以及分类
            $uid = $this->uid;
            //查询出 wiki分类信息
            $wiki_info = Wiki::getWikiBaseInfos([$wiki_id]);
            if (!$wiki_info) {
                throw new \UnexpectedValueException('wiki不存在');
            }
            $wiki_info = $wiki_info[$wiki_id] ?? [];
            $category_id = $wiki_info['category_id'] ?? 0;
            $second_category_id = $wiki_info['second_category_id'] ?? 0;
            $third_category_id = $wiki_info['third_category_id'] ?? 0;
            $four_category_id = $wiki_info['four_category_id'] ?? 0;


            $cate_ids = [
                $category_id,
                $second_category_id,
                $third_category_id,
                $four_category_id,
            ];
            //分类信息
            $cate_ids = array_filter($cate_ids);

            if (!$cate_ids) {
                throw new \UnexpectedValueException('无分类');
            }

            $where['cate_id'] = $cate_ids;
            $parent_ids = ShopCategoryClassify::selectInfos($where, 'parent_id', false);
            if (!$parent_ids) {
                throw new \UnexpectedValueException('无父级无推荐');
            }

            //二级分类
            $info = ShopCategoryClassify::selectInfos(['id' => $parent_ids],
                ['id', 'classify_name', 'cate_id'], true);

            if (!$info) {
                throw new \UnexpectedValueException('二级分类无推荐');
            }
            //随机获取一个二级分类
            $array_key = array_rand($info, 1);
            $cate_info = $info[$array_key];
            if (!$cate_info) {
                throw new \UnexpectedValueException('随机无推荐');
            }
            $classify_id = $cate_info['id'];

            $redis_key = 'find_wiki_' . $classify_id . '_' . $uid;
            //查询是否推送过当前品类 2h内-不重复推荐 60 * 60 * 2
            $lock = \Biz\cache\wiki\Wiki::isLock($redis_key, 60 * 60 * 2);
            if ($lock) {
                throw new \UnexpectedValueException('2h不重复推荐');
            }
            $link = \Doraemon\tools\Hhzurl::H5FindWikiMiddleList($classify_id, $cate_info['classify_name']) ?? '';
            $data = [
                'title' => $cate_info['classify_name'] . '合集',
                'desc' => '根据您的浏览实时推荐',
                'classify_id' => $classify_id,
                'classify_name' => $cate_info['classify_name'],
                'cate_id' => $classify_id,
//                'link' => \Doraemon\tools\LinkFormat::encryptionLink($link)
                'link' => \Biz\tools\helper\Common::formatFullScreenLink($link)
            ];
            $data['statSign'] = basicStatSign::genStatSign(basicStatSign::MODULE_FIND_WIKI_FIND_WIKI_COLLECTION, [],
                [
                    'tab_id' => $first_classify_id,
                    'collection_id' => $classify_id,
                    'classify_name' => $cate_info['classify_name'],
                    'uid' => $this->uid,
                    'act_from' => $act_from,
                ]);
            $wiki_ids = \Biz\service\wiki\FindWIkiService::getWikiIdsByClassifyId($classify_id, 1, 3, $wiki_id);

            if (!$wiki_ids) {
                throw new \UnexpectedValueException('无wiki');
            }

            $wiki_list = Wiki::getWikiBaseList($wiki_ids, $uid);

            if (!$wiki_list) {
                throw new \UnexpectedValueException('无列表');
            }
            foreach ($wiki_list as $k => $v) {
                $wiki_list[$k]['statSign'] = basicStatSign::genStatSign(basicStatSign::MODULE_FIND_WIKI_COLLECTION_WIKI,
                    [],
                    [
                        'wiki_id' => $v['id'],
                        'uid' => $this->uid,
                        'act_from' => $act_from,
                    ]);
            }

            $data['wiki_list'] = array_values($wiki_list);

        } catch (UnexpectedValueException $e) {
            return $this->setError(0, $e->getMessage());
        } catch (Exception $e) {
            \Doraemon\tools\Log::error('获取好物商品合集-error:' . $e->getMessage(), $this->getRequest()->getPost());
            return $this->setSuccess(['msg' => '系统错误: ' . $e->getMessage()], 0);
        }
        $this->setSuccess($data);
    }

}